<fieldset> 


  <label>Vol</label>

	<select class="span2" id="origin_airport" name="origin_airport">
	  <option value="">-- Ville de départ --</option>
	  <?php foreach($originAirports as $airport): ?>
	  	<option value="<?php echo $airport->getCode() ?>"<?php if($origin_airport == $airport->getCode()):?> selected<?php endif; ?>><?php echo $airport->getName() ?></option>
		<?php endforeach; ?>
	</select>

	<select class="span2" id="destination_airport" name="destination_airport">
	  <option id="destination_airport_default" value="">-- Ville de destination --</option>
	  <option id="destination_airport_empty" disabled="disabled">Selectionnez votre ville de depart</option>
	</select>

  <div>
    <label class="checkbox inline">
      <input type="radio" id="flight_type_goback" name="flight_type" value="go-back" checked="checked"> Aller/Retour
    </label>
    <label class="checkbox inline">
      <input type="radio" id="flight_type_go" name="flight_type" value="go"> Aller simple
    </label>
  </div>

	<label>Date aller </label>

	<input type="text" name="date_go_str" value="<?php echo $date_go_str ?>" id="booking_date_go_str"/>
  <div id="date_back_fields">
    <label>Date retour </label>
    <input type="text" name="date_back_str" value="<?php echo $date_back_str ?>" id="booking_date_back_str"/>
  </div>
  <input type="hidden" name="date_go" id="booking_date_go" value="<?php echo $date_go ?>"/>
  <input type="hidden" name="date_back" id="booking_date_back" value="<?php echo $date_back ?>"/>

  <label>Passagers </label>
  <select id="passengers_count" class="span1">
    <?php
    $p = 1;
    while($p < 8)
      {?>
    <option value="<?php echo $p ?>"><?php echo $p ?></option>
      <?php
      $p++;
      }
    ?>
  </select>
  <br/>
  <div id="passengers_list">

  </div>
</fieldset>

<script>
  var destinationAirports  = <?php echo json_encode($destinationAirports->getRawValue()); ?>;
  var destination_airport  = '<?php echo $destination_airport ?>';
  var passenger_categories = <?php echo json_encode($passenger_categories->getRawValue()); ?>;

  function updatePassengers()
  {
    var total_passengers = $('#passengers_count').val();
    $('#passengers_list').empty();

    var selectInputStr = '<select name="passengers[]">';
    for(o in passenger_categories)
    {
      selectInputStr += '<option value="'+passenger_categories[o]['id']+'">'+passenger_categories[o]['name']+'</option>';
    }
    selectInputStr += '</select>';

    for(var p=1; p<=total_passengers; p++)
    {
      $('#passengers_list').append('<div>Passager '+p+' '+selectInputStr+'</div>');
    }
  }

  $(document).ready(function(){
    $.datepicker.setDefaults( $.datepicker.regional[ "fr" ] );

    $('#booking_date_go_str').datepicker({minDate: new Date(<?php echo date('Y') ?>, <?php echo date('m') ?> -1, <?php echo date('d') ?>)});
    $('#booking_date_back_str').datepicker({minDate: new Date(<?php echo date('Y') ?>, <?php echo date('m') ?> -1, <?php echo date('d') ?>)});
    $('#destination_airports select').hide();
    $('#passengers_count').change(function(){updatePassengers()});
    updatePassengers();
    
    var destination_airport_empty   = $('#destination_airport_empty');
    var destination_airport_default = $('#destination_airport_default');

    $('#origin_airport').change(function(){
    		$('#destination_airports select').hide().val('');
    		var airportCode = $(this).val();
    		if(airportCode == '')
    		{
    			$('#destination_airport').empty().append(destination_airport_default).append(destination_airport_empty);
    		}
    		else
    		{
    			$('#destination_airport').empty().append(destination_airport_default);
    			if(destinationAirports[airportCode])
    			{
            var uniqueCodes = {}
      			for(o in destinationAirports[airportCode])
      			{
              var code = destinationAirports[airportCode][o]['code'];
              if(!uniqueCodes[code])
              {
                var name = destinationAirports[airportCode][o]['name'];
                $('#destination_airport').append('<option value="'+code+'">'+name+'</option>');
                uniqueCodes[code] = true;
              }
      			}
    			}
    		}
    })

    $('#flight_type_go').click(function(){
      $('#date_back_fields').hide();
    });

    $('#flight_type_goback').click(function(){
      $('#date_back_fields').show();
    });

    var _form = $('#origin_airport').parents('form');

    _form.bind('submit',function()
    {
      var origin_airport = $('#origin_airport').val();
      var destination_airport = $('#destination_airport').val();
      if(origin_airport == '' || destination_airport == '')
      {
        alert('Veuillez selectionner les villes de depart et de destination');
        return false;
      } 
      var date_go   = $('#booking_date_go_str').val();
      var date_back = $('#booking_date_back_str').val();
      var dg = date_go.split('/');
      var db = date_back.split('/');

      var dgInt = parseInt(dg[2])+parseInt(dg[1])+parseInt(dg[0]);
      var dbInt = parseInt(db[2])+parseInt(db[1])+parseInt(db[0]);

      if($('#flight_type_goback').is(':checked') && dgInt >= dbInt)
      {
        alert('Veuillez selectionner un vol retour ulterieur au vol aller');
        return false;
      }

      $('#booking_date_go').val(dg[2]+'-'+dg[1]+'-'+dg[0]);
      $('#booking_date_back').val(db[2]+'-'+db[1]+'-'+db[0]);

    });
    
  });
</script>